<script src="js/latlon.js">/* Latitude/Longitude formulae */</script>
<script src="js/geo.js">/* Geodesy representation conversions */</script>
<script>

//lat1  59.3299583
//long1 18.0572117
//lat2  59.6461718 
//long2 17.9283508

var R = 6371; // km
var dLat = (59.6461718-59.3299583).toRad();
//var dLat = (lat2-lat1).toRad();
var dLon = (17.9283508-18.0572117).toRad();
//var dLon = (lon2-lon1).toRad();
var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
        Math.cos(59.3299583.toRad()) * Math.cos(59.6461718.toRad()) *
//        Math.cos(lat1.toRad()) * Math.cos(lat2.toRad()) *
        Math.sin(dLon/2) * Math.sin(dLon/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c;

console.debug(d);

//hämta ut distance till andra point.
//dela distance med hur långt tåget åkt
//använd formla längst ner här
//för att kolla var punkt ska vara med distance till hur långt tåget åkt.
//alltså d för distance som jag får genom att ta tiden och använda mot tågens kvarvarande tid.

var y = Math.sin(dLon) * Math.cos(59.6461718);
//var y = Math.sin(dLon) * Math.cos(lat2);
var x = Math.cos(59.3299583)*Math.sin(59.6461718) -
//var x = Math.cos(lat1)*Math.sin(lat2) -
        Math.sin(59.3299583)*Math.cos(59.6461718)*Math.cos(dLon);
//        Math.sin(lat1)*Math.cos(lat2)*Math.cos(dLon);
var brng = Math.atan2(y, x).toDeg();

//console.debug(brng);

var lat2 = Math.asin( Math.sin(lat1)*Math.cos(d/R) +
                      Math.cos(lat1)*Math.sin(d/R)*Math.cos(brng) );
var lon2 = lon1 + Math.atan2(Math.sin(brng)*Math.sin(d/R)*Math.cos(lat1),
                             Math.cos(d/R)-Math.sin(lat1)*Math.sin(lat2));

</script>
<!--<form name="f">
Lat1: <input type="text" name="lat1" id="lat1"> Lon1: <input type="text" name="lon1" id="lon1">
Lat2: <input type="text" name="lat2" id="lat2"> Lon2: <input type="text" name="lon2" id="lon2">
<input type="button" name="calc-dist" id="calc-dist" value="Calculate distance"
  onClick="var p1 = new LatLon(Geo.parseDMS(f.lat1.value), Geo.parseDMS(f.lon1.value));
           var p2 = new LatLon(Geo.parseDMS(f.lat2.value), Geo.parseDMS(f.lon2.value));
           alert(p1.distanceTo(p2));">
</form>-->
